package com.springboot.myspringboot.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.myspringboot.config.Result;
import com.springboot.myspringboot.entity.Solid;
import com.springboot.myspringboot.mapper.SolidMapper;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/solid")
public class SolidController {

    @Resource
    SolidMapper solidMapper;

    @PostMapping("/save")
    public Result<?> getString(@RequestBody Solid solid) {
        solidMapper.insert(solid);
        return Result.success();
    }

    @GetMapping("findPage")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Solid> wrapper = Wrappers.<Solid>lambdaQuery();
        Page<Solid> solidPage = solidMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
        return Result.success(solidPage);
    }

    @GetMapping("findAll")
    public Result<?> findAll(){
        List<Solid> list = solidMapper.findAll();
        return Result.success(list);
    }

    @PutMapping("/update")
    public Result<?> update(@RequestBody Solid solid){
        Integer solidId = solidMapper.updateById(solid);
        if(solidId != 1) {
            return Result.error("500", "不存在id");
        }
        return Result.success(solidId);
    }

    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam Integer id){
        Integer deleteid = solidMapper.deleteById(id);
        if(deleteid != 1){
            return  Result.error("500", "不存在id");
        }
        return Result.success();
    }
}
